import { Config } from "../../src/config/Config";
import { DbConfig } from "../../src/config/DbConfig";
import { Base64Helper } from "../../src/engine/utils/Base64Helper";
import DateUtil from "../../src/engine/utils/DateUtil";
import { Logger } from "../../src/engine/utils/Logger";
import { MysqlClient } from "../../src/engine/utils/MySqlClient";
import { MysqlManager } from "../../src/engine/utils/MysqlManager";

export class RobotHistoryDao {

    public static DB_NAME: string = DbConfig.Db_Name.db_laoyou;

    private static getDbName() {
        // if (Config.environment == Config.EnvironmentType.yueyouhui) {
        //     return DbConfig.Db_Name.db_yuxiaxie2
        // }
        // else if (Config.environment == Config.EnvironmentType.xj) {
        //     return DbConfig.Db_Name.db_yuxiaxie2
        // }
        // else if(Config.environment == Config.EnvironmentType.Jk){
        //     return DbConfig.Db_Name.db_yuxiaxie3
        // }
        // else if(Config.environment == Config.EnvironmentType.chaoyou){
        //     return DbConfig.Db_Name.db_yuxiaxie_chaoyu
        // }
        // else {
            return DbConfig.Db_Name.db_laoyou
        // }
    }

    public static async insertHistory(action: number, robitid: number, operator: string, groupid: number, robot_name: string): Promise<boolean> {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getDbName());
        robot_name = Base64Helper.encode(robot_name)
        // let sql: string = "select * from " + this.DB_NAME + "." + this.Tb_Name + " where `account`=?;"
        let sql: string = 'insert INTO t_robot_history(time,action,robitid, operator, groupid, robot_name) VALUES(?,?,?,?,?,?) ;';
        let time: string = DateUtil.formatDateStr(DateUtil.now())
        let sqlParams = [time, action, robitid, operator, groupid, robot_name];
        let results = await client.queryPromise(sql, sqlParams);
        Logger.log("insertHistory", results)
        if (results && results["affectedRows"] > 0) {
            return true
        } else {
            return false
        }
    }

    public static async get_robot_history(startTime: number, endTime: number, robitid: number) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.getDbName());
        let startTimeStr: string = DateUtil.formatDateStr(startTime)
        let endTimeStr: string = DateUtil.formatDateStr(endTime)
        let sql: string = "select  * from  t_robot_history  where time >= ? and time <= ?"
        if (robitid != 0) {
            sql += " and robitid=" + robitid
        }
        let sqlParams = [startTimeStr, endTimeStr];
        let results = await client.queryPromise(sql, sqlParams);
        Logger.log("get_robot_history=", sql, sqlParams, results)
        if (results && results.length > 0) {
            return results
        } else {
            return []
        }
    }
}